<?php
########################### DBlnk ##################################
class TINY_ADODB {
	var $type = "mysql";
	var $host;
	var $user;
	var $pass;
	var $database;
	var $char = "utf8";
	var $pconnect;
	var $linkId = 0;
	var $arr; 
	var $debug;
	var $dieMethod = 2;###1:return false and continue; # 2:die Msg###
	var $costtime = array();
	var $fetchMode = MYSQL_BOTH;
	var $debuglog;

	function TINY_ADODB($type, $host = NULL, $user = NULL,$pass = NULL, $database = NULL,$char = NULL, $debug = NULL, $pconnect = NULL) {
		if(!is_null($type)) $this->type=$type;
		if(!is_null($host)) $this->host=$host;
		if(!is_null($user)) $this->user=$user;
		if(!is_null($pass)) $this->pass=$pass;
		if(!is_null($database)) $this->database=$database;
		if(!is_null($char)) $this->char=$char;
		if(!is_null($debug))$this->debug = $debug;
		if(!is_null($pconnect))$this->pconnect = $pconnect;
		$this->linkId = $this->Connection();
	}

	function CheckLink($sql){
		if ($sql == '') return false;
		if (!$this -> linkId) return false;
		if ($this -> debug)
			$this->costtime && $this->costtime[0] = $this->GetMicTime();
	}
	
	function Insert_ID(){
		$insertId = mysql_insert_id($this->linkId);
		return $insertId;
	}
	
	function Execute($sql,$unbuffered=0){
		$this->CheckLink($sql);
		if($unbuffered && $this->CheckVersion()>4060){
			$result = @mysql_unbuffered_query($sql,$this->linkId);
		}else{
			$result = @mysql_query($sql,$this -> linkId);
		}
		if($this->debug){
			$this->costtime[1] = $this->GetMicTime();
			$costtime = substr(($this->costtime[1]-$this->costtime[1])*1000,0,10);
			$this->debuglog .= "[Debug]\n执行：$sql\n花费：$costtime 毫秒\n时间：".date("Y-m-d H:i:s")."\n\n";
		}
		if($unbuffered){
			$ret = $result;
		}else{
			if($result==false) return false;
			$ret = new TINY_ADODB_ARRAY($result,$this->fetchMode,$this->dieMethod);
		}
		return $ret;
	}

	function SelectLimit($sql,$offset_b,$offset_n=0){
		$this->CheckLink($sql);
		if(!$offset_n){
			$limit = " limit ".$offset_b;
		}else{
			$limit = " limit ".$offset_b.",".$offset_n;	
		}
		$sql.=$limit;
		$result = $this->Execute($sql);
		return $result;
	}
	
	function GetOne($sql){
		$ret = $this->SelectLimit($sql,1);
		if(!$ret)
			$this->Halt("执行SQL：".$sql."无法返回一个有效的数据集");
		$result = $ret->GetOne();
		return $result;
	}

	function GetRow($sql){
		$row = $this->GetOne($sql);
		return $row;
	}

	function GetAll($sql) {
		$result = $this->Execute($sql);
		if($result){
			$arr = $result->GetAll();
			Return $arr;
		}
		Return false;
	}

	function GetAssoc() {
		$result = $this->Execute($sql);
		if($result){
			while($r = $result->FetchRow()) {
				for($i=1,$max=count($r);$i<$max;$i++){
					$arr[$r[0]] = $arr[$r[$i]];
				}
			}
			if($arr){
				Return $arr;
			}else{
				Return false;
			}
		}
	}

	function Connection() {
		if ($this -> linkId == 0){
			if ($this -> pconnect){
				$this -> linkId = @mysql_pconnect($this->host,$this->user,$this->pass);
			}
			else{
				$this -> linkId = @mysql_connect($this->host,$this->user,$this->pass);
			}
			if (!$this -> linkId){
				$this -> Halt("登录数据库失败。",0);
				return false;
			}

			if (!is_null($this->database) && !@mysql_select_db($this->database,$this->linkId)){
				$this -> Halt("不能使用数据库：".$this -> database);
				return false;
			}
			if ($this -> CheckVersion() >0){
				$sql = "SET NAMES '".$this->char."'";
				$this->Execute($sql,1);
			}
			return $this -> linkId;
		}
	}
	
	function Halt($msg,$mysqlerr=0,$dieErr = true){
		$err = "数据库错误: $msg<br />";
		if($mysqlerr!=0)
			$err.= "MySQL 错误报告: [".$this->ErrorNo()."] ".$this->ErrorMsg()."\n";
		if(1===$this->dieMethod){
			$this->Errorlog .= $err;
			Return false;
		}
		die($err);
	}

	function ErrorMsg() {
		$err= @mysql_error($this -> linkId);
		Return $err;
	}

	function ErrorNo() {
		$err = @mysql_errno($this -> linkId);
		Return $err;
	}

	function CheckVersion(){
		$ret = mysql_get_server_info();
		$pieces = explode("-", $ret);
		$arr = preg_split('/\./',$pieces[0], -1,PREG_SPLIT_NO_EMPTY);
		$ret = $arr[0].$arr[1].$arr[2];
		$ret = (int)$ret;
		return $ret;
	}
	
	function GetMicTime(){ 
    	list($usec, $sec) = explode(" ",microtime()); 
    	return ((float)$usec + (float)$sec); 
	}
	
	function Close(){
        if($this->linkId!=null)
        {
            mysql_close($this->linkId);
            unset($this);
        }
	}
}

######################### 数据集 ################################
class TINY_ADODB_ARRAY{
	var $datastore;
	var $curRow = 0;
	var $fetchMode;
	var $dieMethod;

	function TINY_ADODB_ARRAY(&$data,&$fetchMode,&$dieMethod){
		$this->datastore = &$data;	
		$this->fetchMode = &$fetchMode;
		$this->dieMethod = &$dieMethod;
	}

	function CurrentRow() {
		Return false;
	}
	
	function GetArray(){
		$i=0;
		$result[] = '';
		while($row = mysql_fetch_array($this->datastore,$this->fetchMode)){
			$result[$i]=$row;
			$i++;
		}
		return $result;
	}
	
	function GetAll() {
		$arr = $this->GetArray();
		Return $arr;
	}

	function GetOne(){
		$row = $this->FetchArray();
		return $row;
	}

	function GetAssoc() {
		while($r = $this->FetchRow()) {
			for($i=1;$i<count($r);$i++){
				$arr[$r[0]] = $arr[$r[$i]];
			}
		}
		if($arr){
			Return $arr;
		}else{
			Return false;
		}
	}

	function FetchRow(){
		$row = $this->FetchArray();
		return $row;
	}
	
	function FetchArray(){
		$row = mysql_fetch_array($this->datastore,$this->fetchMode);
		return $row;
	}
	
	function FetchObject(){
		$objrow = mysql_fetch_object($this->datastore);
		return $objrow;
	}
	
	function RecordCount(){
		$rscnt = mysql_num_rows($this->datastore);
		return $rscnt;	
	}

	function RowCount() {
		$rscnt = $this->RecordCount();
		Return $rscnt;
	}
	
	function Affected_Rows(){
		$rscnt = mysql_affected_rows();
		return $rscnt;
	}

	function Move($rownum) {
		$status = @mysql_data_seek($this->datastore,$rownum);
		if($status){
			$this->curRow = $rownum;
			return true;
		}else{
			$this->Halt("Move[seek]($rownum)失败！数据集中共有".$this->RecordCount()."条记录。");
			@mysql_data_seek($this -> datastore,$this->RecordCount());
            $this -> curRow = $this->RecordCount();
            return false;
			
		}
	}

	function MoveFirst() {
		if($this->curRow==0) {
			Return true;
		}else{
			Return $this->Move(0);
		}
	}

	function MoveNext() {
		if($this->curRow<$this->RecordCount()-1) {
			$this->curRow++;
			Return $this-Move($this->curRow);
		}
		Return false;
	}

	function MoveLast() {
		if ($this->RecordCount() >= 0) return $this->Move($this->RecordCount()-1);
		Return false;
	}
	
	function FieldCount() {
		$cnt = mysql_num_fields($this->datastore);
		Return $cnt;
	}

	function FetchField($fieldoffset=0) {
		$meta = mysql_fetch_field($this->datastore,$fieldoffset);
		Return $meta;#obj
	}

	function FetchAllFields() {
		$i = 0;
		while ($i < $this->FieldCount($result)) {
		   $fieldObj[$i] = FetchField($i);
		   $i++;
		}
		Return $fieldObj;
	}

	function Halt($msg,$mysqlerr=0){
		$err = "数据库错误: $msg<br />";
		echo $err;
	}

	function Close(){
		mysql_free_result($this->datastore);
	}
}

####################################数据查询###########################################################
class MyQuery{
	var $db;		//数据库
	var $select;	//查询字段
	var $tbl;		//查询表
	var $where;		//查询条件
	var $order;		//排序方式
	var $limit;		//查询条数
	function MyQuery($db='',$select='*',$tbl='',$where='',$order='',$limit='')
	{
		if(!is_null($db)) $this->db=$db;
		if(!is_null($select)) $this->select=$select;
		if(!is_null($tbl)) $this->tbl=$tbl;
		if(!is_null($where)) $this->where=$where;
		if(!is_null($order)) $this->order=$order;
		if(!is_null($limit)) $this->limit=$limit;
	}
	function setSelect($str)
	{
		if(!is_null($str)) $this->select=$str;
	}
	function setTbl($str)
	{
		if(!is_null($str)) $this->tbl=$str;
	}
	function setWhere($str)
	{
		if(!is_null($str)) $this->where=$str;
	}
	function setOrder($str)
	{
		if(!is_null($str)) $this->order=$str;
	}
	function setLimit($str)
	{
		if(!is_null($str)) $this->limit=$str;
	}
	function getSelect()
	{
		return $this->select;
	}
	function getTbl()
	{
		return $this->tbl;
	}
	function getWhere()
	{
		return $this->where;
	}
	function getOrder()
	{
		return $this->order;
	}
	function getLimit()
	{
		return $this->limit;
	}
	function getSql()
	{
		$sql="select ".$this->select;
		$sql.=" from ".$this->tbl;
		if($this->where!="") $sql.=" where ".$this->where;
		if($this->order!="") $sql.=" order by ".$this->order;
		if($this->limit!="") $sql.=" limit ".$this->limit;
		return $sql;
	}
	function GetAll()
	{
		$db=$this->db;	
		$sql=$this->getSql();
		$aryRow=$db->GetAll($sql);
		return $aryRow;
	}
	
}	
?>